package com.daxt.common.ureport;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bstek.ureport.provider.report.ReportFile;
import com.bstek.ureport.provider.report.ReportProvider;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.daxt.mapper.archives.ArchivesTempletMapper;
import com.daxt.mapper.sys.UreportFileMapper;
import com.daxt.model.constant.BaseConstants;
import com.daxt.model.service.archives.vo.ArchivesTemplet;
import com.daxt.model.service.base.vo.UreportFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * @description: UReport配置
 * @author: lig
 * @date: 2021/5/21
 */
@Component
public class UreportStorage implements ReportProvider {

    private static final String NAME = "档案系统报表存储";

    //特定前缀，ureport底层调用getPrefix方法来获报表操作的Provier类
    private String prefix = "jtlt:";
    private String suffix = ".ureport.xml";

    //是否禁用
    private boolean disable = false;

    @Autowired
    UreportFileMapper ureportFileMapper;
    @Autowired
    ArchivesTempletMapper archivesTempletMapper;

    //加载表报
    public InputStream loadReport(String fileName) {
        QueryWrapper<UreportFile> queryWrapper = new QueryWrapper<UreportFile>();
        queryWrapper.eq("name", getNoCorrectName(fileName));
        UreportFile ureportFileEntity = ureportFileMapper.selectOne(queryWrapper);
//        UreportFileEntity ureportFileEntity = ureportFileMapper.queryUreportFileEntityByName(fileName);
        byte[] content = new byte[0];
        if (ureportFileEntity != null) {
            content = ureportFileEntity.getContent();
        }
        return new ByteArrayInputStream(content);
    }

    //删除表报
    public void deleteReport(String fileName) {
        if (fileName != null) {
            fileName = getNoCorrectName(fileName);


            QueryWrapper<UreportFile> queryWrapper = new QueryWrapper<UreportFile>();
            queryWrapper.eq("name", fileName);
            UreportFile uf = ureportFileMapper.selectOne(queryWrapper);
            archivesTempletMapper.delete(new QueryWrapper<ArchivesTemplet>().eq("templetId",uf.getId()));

            ureportFileMapper.deleteById(uf.getId());
//            ureportFileMapper.delete(queryWrapper);

        }
    }

    //查询所有表报
    public List<ReportFile> getReportFiles() {

        QueryWrapper<UreportFile> queryWrapper = new QueryWrapper<UreportFile>();
        List<UreportFile> ureportFileEntities = ureportFileMapper.selectList(queryWrapper);

//        List<UreportFileEntity> ureportFileEntities = ureportFileMapper.queryReportFileList();
        List<ReportFile> reportFiles = new ArrayList<ReportFile>();
        for (UreportFile ufe : ureportFileEntities) {
            reportFiles.add(new ReportFile(ufe.getName(), ufe.getUpdateTime()));
        }
        return reportFiles;
    }

    //保存表报
    public void saveReport(String fileName, String content) {
        fileName = getNoCorrectName(fileName);

        try {
            QueryWrapper<UreportFile> queryWrapper = new QueryWrapper<UreportFile>();
            queryWrapper.eq("name", fileName);
            UreportFile ureportFileEntity = ureportFileMapper.selectOne(queryWrapper);
            Date date = new Date();
            if (ureportFileEntity == null) {
                ureportFileEntity = new UreportFile();

                ureportFileEntity.setContent(content.getBytes("UTF-8"));

                ureportFileEntity.setUpdateTime(date);
                ureportFileEntity.setCreateTime(date);
                ureportFileEntity.setName(fileName);
                ureportFileEntity.setUrl("ureport/pdf/show?_u="+prefix+ fileName + suffix);
                ureportFileEntity.setIsDelete(BaseConstants.DELETE_NORMAL);
//                ureportFileEntity.setUrl("ureport/preview?_u=jtlt:" + fileName);
                ureportFileMapper.insert(ureportFileEntity);
            } else {
//            ureportFileEntity.setContent(content.getBytes());
                ureportFileEntity.setName(fileName);
                ureportFileEntity.setContent(content.getBytes("UTF-8"));
                ureportFileEntity.setUrl("ureport/pdf/show?_u="+prefix+ fileName + suffix);
                ureportFileEntity.setUpdateTime(date);
                ureportFileMapper.updateById(ureportFileEntity);
            }

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }



    }


    /**
     * 获取没有前缀的文件名
     *
     * @param name
     * @return
     */
    private String getNoCorrectName(String name) {
        if (name.startsWith(prefix)) {
            name = name.substring(prefix.length(), name.length());
            name = name.replace(suffix,"");
        }
        return name;

    }


    public String getName() {
        return NAME;
    }

    public boolean disabled() {
        return disable;
    }

    public String getPrefix() {
        return prefix;
    }


}
